package user.ucm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import user.view.MainView;

public class AirlineInformation implements UCController {

    private MainView mv;

    public AirlineInformation() {
    }

    public AirlineInformation(MainView mv) {
        this.mv = mv;
        this.mv.getAirVModel().getAirName_TextField().setText(this.mv.getAirVModel().getAirplane_List().getSelectedValue().toString());
    }

    public int run() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cp3120?user=root&password=root");
            con.setCatalog("cp3120");

            Statement statement = con.createStatement();
            String search = "SELECT * FROM airinfo WHERE AirlineName='" + 
                    String.valueOf(mv.getAirVModel().getAirplane_List().getSelectedValue()) + "'";
            System.out.println(search);

            ResultSet test = statement.executeQuery(search);
            if (!test.first()) {
                return 3;
            } else {
                test.beforeFirst();
                test.next();
                this.mv.getAirVModel().getDestination_TextField().setText(test.getString("AirlineDestine"));
                this.mv.getAirVModel().getSetOut_TextField().setText(test.getString("AirlineTime"));
                this.mv.getAirVModel().getPrice_TextField().setText(test.getString("AirlinePrice"));
                return 0;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
            return 2;
        }
    }
}
